Welcome to CRUD Operations.
After watching this video, you will be able to: Use mongo shell to connect
to your MongoDB database, and perform basic CRUD operations.
Mongo shell is a command line tool provided by MongoDB to interact with your databases.
It is an interactive JavaScript interface, and you can use it to perform data and
administrative operations on MongoDB. So, first we connect to our cluster
by providing a connection string, And once connected, we can view
the list of all our databases. For simplicity, we will assume the shell is
already connected to our MongoDB instance. To start working on the campus management
database, we run the following statement: And then we can see what
collections are present in the campus management database using this command: It shows two collections: Staff and Students.
CRUD operations are Create, Read, Update, and Delete operations, hence the acronym CRUD.
Let’s start with a Create operation. We will insert a new document
into the Students collection. It takes the form of ‘db.’, which is the
campusManagementDB database. Then the collection name, which is ‘students.’
And then the ‘insertOne’ function, Which takes an argument as the document we
want to create in the ‘Students’ collection. The outcome is an acknowledgment
that the operation was successful. And since this was an insert
operation, it shows ‘insertedId’. ‘_id’ <pronounced as “underscore id”>is a
required field on every document in MongoDB. Because we didn’t provide one ourselves in John
Doe’s document, the Mongo shell driver will add the ‘_id’ field and generate the ‘ObjectId’ for
us. Mongo shell is a javascript interpreter, meaning you can define variables and
perform other functions in it too. Here, ‘students_list’ is created with two
documents in it, and we can pass this as an argument to the ‘insertMany’ function.
Now let’s do some Read operations. In the first one, we call the ‘findOne’
function, without any filter criteria. This will return the first document in
natural order, which is the order in which the database refers to documents on disk.
In this second Read operation, we want to find the first student with a specific email address.
If more than one document matches the criteria, only the first one will be read, as
we are using the ‘findOne’ function. Next, we want to retrieve all students
with the lastname of ‘Doe’. And finally, if we just want to count how many students have
the lastname ‘Doe’, we call the ‘count’ function. This count will happen on MongoDB, and only the
number value will be returned. Now we will perform a Replace operation. Let’s retrieve a student
record and make some changes to the document. We are creating a new field to identify which
students are on campus or online only. We will also update the email with campus’ email.
Once all changes are made, we will call the ‘replaceOne’ function,
Which takes as its first argument, the filter criteria to find a document with last name of Doe.
The second argument is the updated student object. The outcomes of this statement are:
An acknowledgement The information about how many documents
matched our filter criteria, and The count of how many were actually modified. In the Replace
example, we were sending the whole document back with the new changes. For larger documents,
this will take a lot of transfer time between the client and the database. Instead, we can use
MongoDB’s in-place updates for small changes. So, we do this by constructing a ‘changes’
document, which in our case only has two assignments, which come under ‘$set’.
Then we call the ‘updateOne’ function. To update all of the students to online only
due to lockdown, we can run the ‘updateMany’ function without any filter criteria, and a
change document. To run a delete operation, like update and find, the ‘deleteOne’ function
takes filter criteria as a first argument. Running that delete statement provides you with
an acknowledgment and a number representing how many documents were deleted.
Similarly, you have a deleteMany function to delete more than one
document meeting the criteria. In this video, you learned that:
The Mongo shell is an interactive command line tool provided by MongoDB
to interact with your databases. To use the Mongo shell, you
first need to make a connection to your cluster via a connection string.
You use ‘show dbs’ to list databases, ‘use databasename’ to select a database, and ‘show
collections’ to list collections in a database. CRUD operations consist of
Create, Read, Update, and Delete. Useful functions include insertOne, insertMany,
findOne, find, count, replace, updateOne, updateMany, deleteOne, and deleteMany. Different kinds of acknowledgements are
returned based on the operation being run.